package outoforder.leetcode;

import org.junit.Test;

/**
 * @author shhjiang_001
 * @create 2020-07-01 17:15
 */
public class Solution122 {
    public int maxProfit1(int[] prices) {
        int[][] ints = new int[prices.length][2];
        ints[0][0] = 0;
        ints[0][1] = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            ints[i][1] = Math.max(ints[i-1][1],ints[i-1][0]-prices[i]);
            ints[i][0] = Math.max(ints[i-1][0],ints[i-1][1]+prices[i]);
        }
        return ints[prices.length-1][0];
    }
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int dp_i_0 = 0;
        int dp_i_1 = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            dp_i_0 = Math.max(dp_i_0,dp_i_1+prices[i]);
            dp_i_1 = Math.max(dp_i_0-prices[i],dp_i_1);
        }
        return dp_i_0;
    }
    @Test
    public void test(){
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
}
